package com.ww.disruptor.producer;

import com.lmax.disruptor.RingBuffer;
import com.ww.disruptor.event.OrderEvent;
import lombok.AllArgsConstructor;

import java.nio.ByteBuffer;

public class OrderEventProducer {

    private RingBuffer<OrderEvent> ringBuffer;

    public OrderEventProducer(RingBuffer<OrderEvent> ringBuffer) {
		super();
		this.ringBuffer = ringBuffer;
	}

	public void sendData(ByteBuffer data) {
        // 1、在生产者发送消息的时候，首先需要从我们的ringBuffer里面获取一个可用的序号
        long sequence = ringBuffer.next();

        try {
            // 2、根据这个需要找到具体的OrderEvent，此时获取的对象还没被赋值
            OrderEvent event = ringBuffer.get(sequence);
            // 3、进行实际的赋值处理
            event.setValue(data.getLong(0));
        } finally {
            // 4、提交发布操作
            ringBuffer.publish(sequence);
        }
    }
}
